#!/bin/bash
# SPDX-License-Identifier: GPL 2.0
#
# This is a small script to generate device node entries at /dev for
# NKCAN devices. The script uses the entries from /proc/devices.
# This means the driver have to be installed before using the script.
#
#
group="root"
mode="666"
usage="Usage: nkcan_make_devices count-of-device-node-for-each-type [--help | -h]"

if test $# -gt 0; then
  case $1 in
  [1-8])  ;;
  "--help" | "-h" | "-?" | "/?")
          echo ""
          echo $usage
          echo ""
          echo "Info: Create \"count-of-device-node-for-each-type\" device nodes for each NKCAN-type."
          echo ""
          echo ""
          echo ""
          echo ""
          exit 1 ;;
  "--debug" | "-d") DEBUG=1 ;;
  esac
fi

if test $UID -ne 0; then
  echo "ERROR: you must be root to use nkcan_make_devices!"
  echo ""
  exit 1
fi

# check command line arguments
if test $# -le 0; then
  echo "ERROR: please provide the number of devices per interface type!"
  echo $usage
  echo ""
  exit 1
fi

# get major number from /proc/devices
major=`cat /proc/devices | awk "\\$2==\"nkcan\" {print \\$1}"`

# make device nodes
if test "$major"; then
  j=0;
  while test $j -le 44; do
    k=$[$j + ($1 - 1)];
    i=$j;
    while test $i -le $k; do
      rm -f /dev/nkcan$i;
      mknod /dev/nkcan$i c $major $i;
      chgrp $group /dev/nkcan$i;
      chmod $mode  /dev/nkcan$i;
      i=$[$i + 1];
    done;
  j=$[$j + 8];
  done;
else
  echo "Info: Please do first a \"modprobe nkcan.o or modprobe nkcan.ko .. (depends on kernel version)\"";
fi

# end of script

